<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="统计年份" style="width: 308px">
        <el-date-picker
          v-model="daterangeYear"
          value-format="YYYY"
          type="daterange"
          range-separator="-"
          start-placeholder="开始年份"
          end-placeholder="结束年份"
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="种植主体" prop="planterId">
        <el-select v-model="queryParams.planterId" placeholder="请选择种植主体" clearable style="width: 240px">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="服务月份" style="width: 308px">
        <el-date-picker
          v-model="daterangeServiceMonth"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="服务类型" prop="serviceType">
        <el-select v-model="queryParams.serviceType" placeholder="请选择服务类型" clearable style="width: 240px">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="服务次数" prop="serviceCount">
        <el-input
          v-model="queryParams.serviceCount"
          placeholder="请输入服务次数"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['statistics:plantService:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="plant_serviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="记录id" align="center" prop="id" />
      <el-table-column label="数据统计年份" align="center" prop="year" width="180">
        <template #default="scope">
          <span>{{ scope.row.year }}</span>
        </template>
      </el-table-column>
      <el-table-column label="种植主体" align="center" prop="planterName" />
      <el-table-column label="服务月份" align="center" prop="serviceMonth" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.serviceMonth, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务类型" align="center" prop="serviceType" />
      <el-table-column label="服务次数" align="center" prop="serviceCount" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script setup name="PlantService">
import { listPlantService } from "@/api/statistics/plant_service";

const { proxy } = getCurrentInstance();

const plant_serviceList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeYear = ref([]);
const daterangeServiceMonth = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    year: null,
    planterId: null,
    serviceMonth: null,
    serviceType: null,
    serviceCount: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询种植主体服务统计列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeYear && '' != daterangeYear) {
    queryParams.value.beginYear = daterangeYear.value[0];
    queryParams.value.endYear = daterangeYear.value[1];
  }
  if (null != daterangeServiceMonth && '' != daterangeServiceMonth) {
    queryParams.value.beginServiceMonth = daterangeServiceMonth.value[0];
    queryParams.value.endServiceMonth = daterangeServiceMonth.value[1];
  }
  listPlantService(queryParams.value).then(response => {
    plant_serviceList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    id: null,
    year: null,
    planterId: null,
    serviceMonth: null,
    serviceType: null,
    serviceCount: null,
  };
  proxy.resetForm("plant_serviceRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeYear.value = [];
  daterangeServiceMonth.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据  */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}


/** 导出按钮操作 */
function handleExport() {
  proxy.download('statistics/plant_service/export', {
    ...queryParams.value
  }, `plant_service_${new Date().getTime()}.xlsx`);
}

/** 是否渲染字段 */
function renderField(insert, edit) {
  return form.value.id == null ? insert : edit;
}

getList();
</script>